<?php
$loader->requireOnce('includes/OutPatientFlow/TicketModel.class.php');
class OutPatientFlowProcessor {
	private static $widths =   array(15, 160, 15);
	public static function renderTicketItemListHMLT($ticketId) {
        $objTicketModel =   new TicketModel($ticketId);
        $itemModelList  =   $objTicketModel->getItemList();
        $html   =   '';
        if (count($itemModelList) > 0) {
            $serviceHTML        =   '';
            $paraCliniqueHTML   =   '';
            foreach ($itemModelList as $itemModel) {
                if (($itemModel->getItemType() == TicketItem::SERVICE_TYPE)
                        || ($itemModel->getItemType() == TicketItem::COMPOSITE_SERVICE)) {
                    $itemHTML       =   self::renderServiceItemHTML($itemModel);
                    $serviceHTML    .=  $itemHTML;
                }
                else {
                	$itemHTML           =   self::renderParaclinicalItemHTML($itemModel);
                    $paraCliniqueHTML   .=  $itemHTML;
                }
            }
            if (!empty($serviceHTML)) {
            	$html  .=   "<div><div class='group_title'>" . L10nHelper::_('Examination-Surgery') . "</div>";
            	$html  .=  $serviceHTML;
            	$html  .=  "</div>";
            }
            if (!empty($paraCliniqueHTML)) {
                $html  .=   "<div><div class='group_title'>" . L10nHelper::_('Paraclinique'). "</div>";
                $html  .=  $paraCliniqueHTML;
                $html  .=  "</div>";
            }
        }

        return $html;
	}

	private static function renderServiceItemHTML($itemModel) {
        $w0     =   self::$widths[0];
        $w1     =   self::$widths[1];
        $w2     =   self::$widths[2];
		$class  =   '';
        $hidden =   '';
        if (EnforceType::int($itemModel->getQueueId()) > 0) {
        	$class     =   "active";
        	$hidden    =   "<input type='hidden' name='currentItemId' id='currentItemId' value='" . $itemModel->getTicketItemId() . "'/>";
        }
		$html   =   "<div class='$class' id='" . $itemModel->getTicketItemId() ."'>$hidden<table class='ticketItemTable' cellpadding='0' cellspacing='0'><tbody><tr>";
        if ($itemModel->getStatus() == TicketItem::CLOSED) {
            $html   .=  "<td style='width: {$w0}px; text-align: left;'><img src='../images/yes.png'/></td>";
            $html   .=  "<td style='width: {$w1}px;'>" . $itemModel->getServiceName() . "</td>";
            $html   .=  "<td style='width: {$w2}px;'>&nbsp;</td>";
        }
        else {
        	$html   .=  "<td style='width: {$w0}px;'>&nbsp;</td>";
            $html   .=  "<td style='width: {$w1}px;'>" . $itemModel->getServiceName() . "</td>";
            $html   .=  "<td style='width: {$w2}px; text-align:right;'><a href='javascript:deleteTicketItem(" . $itemModel->getTicketItemId() . ")' title='". L10nHelper::_('Delete') . "'><img src='../images/remove_small_16x16.png'/></a></td>";
        }
        $html   .=  "</tr></tbody></table></div>";

        return $html;

	}

	private static function renderParaclinicalItemHTML($itemModel) {
        $html   =   "<div id='" . $itemModel->getTicketItemId() . "'><table class='ticketItemTable' cellpadding='0' cellspacing='0'><tbody><tr>";
        $w0     =   self::$widths[0];
        $w1     =   self::$widths[1];
        $w2     =   self::$widths[2];
        if ($itemModel->getStatus() == TicketItem::CLOSED) {
            $html   .=  "<td style='width: {$w0}px; text-align: left;'><img src='../images/yes.png'/></td>";
            $html   .=  "<td style='width: {$w1}px;'>" . $itemModel->getServiceName() . "</td>";
            $html   .=  "<td style='width: {$w2}px;'>&nbsp;</td>";
        }
        else if ($itemModel->getStatus() == TicketItem::OPEN){
            $html   .=  "<td style='width: {$w0}px;'>&nbsp;</td>";
            $html   .=  "<td style='width: {$w1}px;'>" . $itemModel->getServiceName() . "</td>";
            $html   .=  "<td style='width: {$w2}px; text-align:right;'><a href='javascript:deleteTicketItem(" . $itemModel->getTicketItemId() . ")' title='". L10nHelper::_('Delete') . "'><img src='../images/remove_small_16x16.png'/></a></td>";
        }
        else {
            $html   .=  "<td style='width: {$w0}px;'>&nbsp;</td>";
            $html   .=  "<td style='width: {$w1}px;'>" . $itemModel->getServiceName() . "</td>";
            $html   .=  "<td style='width: {$w2}px;'></td>";
        }
        $html   .=  "</tr></tbody></table></div>";

        return $html;
	}

	public static function deleteTicketItem($ticketItemId) {
		$result = array('error'  => array(
                                'code'  =>  CoreErrorCode::NORMAL,
                                'mess'=>''),
                        'result' => ''
        );
        $objTicketItem  =   Celini::newORDO('TicketItem', $ticketItemId);
        $itemType       =   $objTicketItem->get('item_type');
        if ($itemType == TicketItem::TEST_TYPE || $itemType == TicketItem::ENDO_TYPE
                || $itemType == TicketItem::XRAY_TYPE || $itemType == TicketItem::ULTRA_TYPE) {
            $result =   ParacliniqueEngine::deleteParaClinicTicketItem($ticketItemId);
        }
        else {
            $objTicketItem->delete($ticketItemId);
        }

        return $result;
	}

	public static function performTicketItem($ticketItemId) {
        //  Force the next item in approciate queue (Cashier queue or Encounte queue)
        $objTicketItem  =   ORDataObject::factory('TicketItem');
        if ($objTicketItem->isPaymentType($ticketItemId, Service::BEFORE) == true) {
            // Cashier queue
            $queue = AbsQueue::getQueue(AbsQueue::CASHIER);
            $queue->addTicketItem($ticketItemId);
        } else {
            // Encounter queue
            $queue = AbsQueue::getQueue(AbsQueue::ENCOUNTER);
            $queue->addTicketItem($ticketItemId);
        }
	}

	public static function closeItems($ticketId) {
        $objTicketItem  =   ORDataObject::factory('TicketItem');
		$itemArray        =   $objTicketItem->getItemsFromTicket($ticketId);
		// Close all the ticket items
		foreach ($itemArray as $item) {
            $ticketItemId	= 	$item['ticket_item_id'];
            $itemType		=	$item['item_type'];
            if ($itemType == TicketItem::TEST_TYPE || $itemType == TicketItem::ENDO_TYPE
                    || $itemType == TicketItem::XRAY_TYPE || $itemType == TicketItem::ULTRA_TYPE) {
                $result =   ParacliniqueEngine::finishParaClinicTicketItem($ticketItemId);
            }
            else {
                $objTicketItem->changeStatus($ticketItemId, TicketItem::CLOSED);
                $objTicketItem->close($ticketItemId);
            }
		}
        // Remove items out of the queue
        ORDataObject::factory('QueueItem')->removeItemsByTicket($ticketId);
	}
}
?>